// import HttpService from "./HttpService";

import { GameManager } from "../../script/game/GameManager";

const { ccclass, property } = cc._decorator;

export enum AudioId {
/** bg*/ bgm,
/** bg2*/ bg2,
/** btnSound*/ btnSound,
}
@ccclass
export default class AudioMgr /* extends cc.Component */ {
    public static readonly instance = new AudioMgr();
    private constructor() { }

    private mBgmAudioId = -1; /** 是否第一次播放 播放音乐id*/
    public mBgmVolume = 0.75; // 默认背景声音大小
    public mSfxVolume = 0.75; // 默认音效声音大小
    private uid = 0; // 用户id

    init() {
        this.uid = cc.sys.localStorage.getItem("uid") || 0; // 获取uid 根据每个用户进行定制声音播放情况

        let bv = cc.sys.localStorage.getItem("mBgmVolume" + this.uid);
        if (bv) this.mBgmVolume = parseFloat(bv);
        let bs = cc.sys.localStorage.getItem("mSfxVolume" + this.uid);
        if (bs) this.mSfxVolume = parseFloat(bs);
        // console.log("type:", SDKManager.phoneType, "mBgmVolume", bv, this.mBgmVolume, "mSfxVolume:", bs, this.mSfxVolume);
    }
    public lateUpdate() { 
        if (cc.sys.isBrowser) {
            let context = (<any>cc.sys).__audioSupport.context;
            if (context.state === 'suspended') { // google chrome latest version need set running 
                context.resume().then(e => {
                    console.log(context.state);
                });
                console.log(context.state);
            }
        }
    }


    private getUrl(url: string): string { // 统一获取url
        let addPath = "";
        if (url.indexOf("_") != -1) addPath = "sj/";
        // return cc.url.raw("resources/sounds/" + addPath + url + ".m" + "p3");
        return "sounds/" + addPath + url;
    }

    async playBGM(id: AudioId | string | cc.AudioClip = AudioId.bgm) {
        let audioUrl = id instanceof cc.AudioClip ? id : this.getUrl(typeof id == "string" ? id : AudioId[id]);
        if (this.mBgmAudioId >= 0) cc.audioEngine.stop(this.mBgmAudioId);
        if (this.mBgmVolume == 0) return;
        if (!this.isSupportAudio()) return; // 不支持播放音乐
        cc.audioEngine.stopAll();//.stopMusic(); // 这里应该放在load的播放前
        this.mBgmAudioId = await this.play(audioUrl, true, this.mBgmVolume); // cc.audioEngine.play(audioUrl, true, this.mBgmVolume);
        console.log(this.mBgmAudioId, audioUrl)
    }
    public isSupportAudio(): boolean {
        if (!cc.sys.isBrowser) return true;
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            return true;
        } else {
            if ((<any>cc.sys).__audioSupport.WEB_AUDIO && !((<any>cc.sys).__audioSupport.context && (<any>cc.sys).__audioSupport.context['createGain'])) return false;
            return true;
        }
    }
    playSFXById(id: AudioId | cc.AudioClip, isMan: boolean = true) {
        // let url = "cc-" + (isMan ? 1 : 0) + "-" + id;
        let audioUrl = id instanceof cc.AudioClip ? id : AudioId[id];
        return this.playSFX(audioUrl);
    }
    async playSFX(url: string | cc.AudioClip) {
        let audioUrl = url instanceof cc.AudioClip ? url : this.getUrl(url);
        // console.log("playSFX:",audioUrl, this.mSfxVolume);
        if (!this.isSupportAudio()) return; // 不支持播放音乐
        if (this.mSfxVolume > 0) return await this.play(audioUrl, false, this.mSfxVolume); // cc.audioEngine.play(audioUrl, false, this.mSfxVolume);
    }

    private async play(url: string | cc.AudioClip, loop, volume): Promise<number | any> {
        return new Promise((resolve, reject) => {
            if (url instanceof cc.AudioClip) {
                let audioID = cc.audioEngine.play(url, loop, volume);
                if (audioID != null) this.lateUpdate(); 
                resolve(audioID);
                return;
            }
            cc.loader.loadRes(url, cc.AudioClip, (err, clip) => {
                let audioID = cc.audioEngine.play(clip, loop, volume);
                if (audioID != null) this.lateUpdate();
            
                resolve(audioID);
            });
        });
 
    }

   
}